package com.gxa.p2p.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.pojo.UserInfo;
import com.gxa.p2p.pojo.UserLockStatus;
import com.gxa.p2p.service.UserLockStatusService;
import com.gxa.p2p.util.MapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * TODO
 *
 * @author Longlin_Feng
 * @date 2020/8/5
 */
@Controller
@CrossOrigin
@RequestMapping("/loanerManage")
@Api(value = "用户锁定状态控制器", tags = "用户锁定状态设置接口")
public class LoanerManageController {

  @Resource private UserLockStatusService userLockStatusService;

  @PutMapping("/setLockStatus")
  @ResponseBody
  @ApiOperation(value = "用户锁定状态设置接口", httpMethod = "put", response = Map.class)
  @ApiImplicitParam(
      name = "paramMap",
      value = "map:包含userInfo和userLockStatus两个键,值分别为基本信息和状态信息",
      required = true,
      paramType = "Map<String, Object>",
      example =
          "{'userInfo':{'realName':'张小三','idCard':'xxxxx'},'userLockStatus':{'lockStatus':'正常','note':'xxxxxx'}}")
  public Map<String, Object> setUserLockStatus(@RequestBody Map<String, Object> paramMap) {

    // 解析UserInfo , UserLockStatus 对象
    ObjectMapper objectMapper = new ObjectMapper();

    UserInfo userInfo = objectMapper.convertValue(paramMap.get("userInfo"), UserInfo.class);
    UserLockStatus userLockStatus =
        objectMapper.convertValue(paramMap.get("userLockStatus"), UserLockStatus.class);
    // 调用服务层设置锁定状态接口方法
    if (userLockStatusService.setUserLockStatus(userInfo, userLockStatus)) {
      MapUtil mapUtil = new MapUtil();
        mapUtil.setCode(0);
      return mapUtil.getMap();
    } else {
      throw new BusinessException(ErrorCode.FAILED);
    }
  }
}
